package com.example.ykbboom.universitytransportation.Activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.ykbboom.universitytransportation.Interface.HandlePictureListener;
import com.example.ykbboom.universitytransportation.R;
import com.example.ykbboom.universitytransportation.Util.ImageUtil;
import com.example.ykbboom.universitytransportation.Util.LogUtil;
import com.example.ykbboom.universitytransportation.Util.NetUtil;
import com.example.ykbboom.universitytransportation.Util.SharedPrefUtil;
import com.example.ykbboom.universitytransportation.Util.ToastUtil;

/**
 * @author ykbboom
 * @email ykbpro@whut.edu.cn
 * @time 2017-2-28 17:32:56
 * 功能:发布请求
*/
public class PublishRequestActivity extends BaseActivity {

    private static final int REQUEST_PLACE_CODE = 1;
    private static final int DESTINATION_PLACE_CODE = 2;
    private static final String TAG = "PublishRequestActivity";
    private Intent intent;
    private Toolbar mToolBar;
    private Button publishReqBtn;
    private TextView placeText;
    private TextView destinationText;
    private LinearLayout placeLinearLayout;
    private LinearLayout choosePicLinearLayout;
    private LinearLayout destinationLinearLayout;
    private ImageView requestImageView;
    private ImageUtil imageUtil;
    private ProgressDialog progressDialog;
    private EditText requestDescText;
    private String latlongStart;
    private String latlongDes;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish_request);
        initView();
    }

    @Override
    public void initView() {
        mToolBar = (Toolbar) findViewById(R.id.toolbar_publish_request);
        setSupportActionBar(mToolBar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        publishReqBtn = (Button) findViewById(R.id.publish_req_btn);
        publishReqBtn.setOnClickListener(this);
        placeLinearLayout = (LinearLayout) findViewById(R.id.location_ll);
        placeLinearLayout.setOnClickListener(this);
        choosePicLinearLayout = (LinearLayout) findViewById(R.id.choose_pic_ll);
        choosePicLinearLayout.setOnClickListener(this);
        destinationLinearLayout = (LinearLayout) findViewById(R.id.location_ll_destination);
        destinationLinearLayout.setOnClickListener(this);
        placeText = (TextView) findViewById(R.id.request_place_text);
        destinationText = (TextView) findViewById(R.id.modify_distance_text);
        requestDescText = (EditText) findViewById(R.id.request_desc_text);
        requestImageView = (ImageView) findViewById(R.id.request_photo_view);
        imageUtil = new ImageUtil(requestImageView,PublishRequestActivity.this);
        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("正在上传图片");
        progressDialog.setMessage("wait...");
        progressDialog.setCancelable(false);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.publish_req_btn:
                if (requestImageView.getDrawable() == null){
                    ToastUtil.showToast(this, "请选择图片。");
                    break;
                }else{
                    imageUtil.uploadPhoto(progressDialog,new HandlePictureListener() {
                        @Override
                        public void onStart(){}

                        @Override
                        public void onStop(String picUrl,Boolean isUploaded,String time) {
                            if (isUploaded){
                                progressDialog.dismiss();
                                String desc = requestDescText.getText().toString();
                                NetUtil.publishReq(picUrl,desc,placeText.getText().toString(),destinationText.getText().toString()
                                        ,time, SharedPrefUtil.getUserPhone(),latlongStart,latlongDes,PublishRequestActivity.this);
                            }
                        }

                        @Override
                        public void onError(Exception e) {
                            progressDialog.dismiss();
                            LogUtil.e(TAG,e.toString());
                            ToastUtil.showToast(PublishRequestActivity.this,e.toString());
                        }
                    });
                    break;
                }

            case R.id.location_ll:
                intent = new Intent(PublishRequestActivity.this,ChoosePlaceActivity.class);
                startActivityForResult(intent, REQUEST_PLACE_CODE);
                break;
            case R.id.choose_pic_ll:
                imageUtil.choosePhoto();
                break;
            case R.id.location_ll_destination:
                intent = new Intent(PublishRequestActivity.this,ChoosePlaceActivity.class);
                startActivityForResult(intent, DESTINATION_PLACE_CODE);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode){
            case REQUEST_PLACE_CODE:
                if (resultCode==RESULT_OK){
                    placeText.setText(data.getStringExtra("place"));
                    latlongStart = data.getStringExtra("latlong");
                }
                break;
            case DESTINATION_PLACE_CODE:
                if (resultCode==RESULT_OK){
                    destinationText.setText(data.getStringExtra("place"));
                    latlongDes = data.getStringExtra("latlong");
                }
                break;
            case ImageUtil.TAKE_PIC:
                if(resultCode==RESULT_OK){
                    //将拍摄的照片进行裁剪
                    imageUtil.cropPic();
                    break;
                }
            case ImageUtil.COMPLETE_CROP_CODE:
                imageUtil.setImageToView(data);
                break;
            case ImageUtil.SELECT_PIC_KITKAT:
                if (data==null){//如果没有选择图片
                    break;
                }else {
                    imageUtil.bitmapFactory(data.getData());
                    break;
                }
            case ImageUtil.IMAGE_REQUEST_CODE:
                imageUtil.startPhotoZoom(data.getData());
                break;
        }
    }
}
